
using System;
using Qdb;
using Qdb.Config;
using Qdb.Internal;
using Qdb.Internal.Config;

namespace Qdb
{
	/// <summary>Factory class to open qdb instances in embedded
	/// mode.</summary>
	/// <remarks> Factory class to open qdb instances in embedded mode.
	/// </remarks>
	/// <seealso cref="Qdb.CS.QdbClientServer"> Qdb.CS.QdbClientServer in
	/// Qdb.CS.dll for methods to open qdb servers and qdb
	/// clients.</seealso>
	/// <since>7.5</since>
	public class QdbEmbedded
	{
		/// <summary>
		/// Creates a fresh
		/// <see cref="Qdb.Config.IEmbeddedConfiguration">IEmbeddedConfiguration</see>
		/// instance.
		/// </summary>
		/// <returns>a fresh, independent configuration with all options set to their default values
		/// 	</returns>
		public static IEmbeddedConfiguration NewConfiguration()
		{
			return new EmbeddedConfigurationImpl(QdbFactory.NewConfiguration());
		}

		/// <summary>
		/// opens an
		/// <see cref="Qdb.IObjectContainer">IObjectContainer</see>
		/// on the specified database file for local use.
		/// <br/>
		/// <br/>
		/// A database file can only be opened once, subsequent attempts to
		/// open another
		/// <see cref="Qdb.IObjectContainer">IObjectContainer</see>
		/// against the same file will result in a
		/// <see cref="Qdb.Ext.DatabaseFileLockedException"> DatabaseFileLockedException</see>
		/// .
		/// <br/>
		/// <br/>
		/// Database files can only be accessed for readwrite access from one
		/// process at one time. All versions except for qdb mobile edition
		/// use an internal mechanism to lock the database file for other
		/// processes.
		/// <br/>
		/// <br/>
		/// </summary>
		/// <param name="config">
		/// a custom
		/// <see cref="Qdb.Config.IConfiguration">IConfiguration</see>
		/// instance to be obtained via
		/// <see cref="newConfiguration">newConfiguration</see>
		/// </param>
		/// <param name="databaseFileName">an absolute or relative path to the database
		/// file</param>
		/// <returns>
		/// an open
		/// <see cref="Qdb.IObjectContainer">IObjectContainer</see>
		/// </returns>
		/// <seealso cref="Qdb.Config.IConfiguration.ReadOnly">
		/// Qdb.Config.IConfiguration.ReadOnly</seealso>
		/// <seealso cref="Qdb.Config.IConfiguration.Encrypt"> Qdb.Config.IConfiguration.Encrypt
		/// </seealso>
		/// <seealso cref="Qdb.Config.IConfiguration.Password">
		/// Qdb.Config.IConfiguration.Password</seealso>
		/// <exception cref="Qdb.Ext.QdbIOException"> I/O operation failed or was unexpectedly
		/// interrupted.</exception>
		/// <exception cref="Qdb.Ext.DatabaseFileLockedException"> the required database file is locked by
		/// another process.</exception>
		/// <exception cref="Qdb.Ext.IncompatibleFileFormatException">
		/// runtime
		/// <see cref="Qdb.Config.IConfiguration">configuration</see>
		/// is not compatible with the configuration of the database file.
		/// </exception>
		/// <exception cref="Qdb.Ext.OldFormatException">
		/// open operation failed because the database file is in old format
		/// and
		/// <see cref="Qdb.Config.IConfiguration.AllowVersionUpdates">
		/// Qdb.Config.IConfiguration.AllowVersionUpdates</see>
		/// is set to false.
		/// </exception>
		/// <exception cref="Qdb.Ext.DatabaseReadOnlyException"> database was configured as read-only.
		/// </exception>
		public static IEmbeddedObjectContainer OpenFile(IEmbeddedConfiguration config, string
			 databaseFileName)
		{
			if (null == config)
			{
				throw new ArgumentNullException();
			}
			return ObjectContainerFactory.OpenObjectContainer(config, databaseFileName);
		}

		/// <summary>
		/// Same as calling
		/// <see cref="OpenFile(Qdb.Config.IEmbeddedConfiguration, string)">OpenFile(Qdb.Config.IEmbeddedConfiguration, string)
		/// 	</see>
		/// with a fresh configuration (
		/// <see cref="NewConfiguration()">NewConfiguration()</see>
		/// ).
		/// </summary>
		/// <param name="databaseFileName">an absolute or relative path to the database file</param>
		/// <seealso cref="OpenFile(Qdb.Config.IEmbeddedConfiguration, string)">OpenFile(Qdb.Config.IEmbeddedConfiguration, string)
		/// 	</seealso>
		/// <exception cref="Qdb.Ext.QdbIOException"></exception>
		/// <exception cref="Qdb.Ext.DatabaseFileLockedException"></exception>
		/// <exception cref="Qdb.Ext.IncompatibleFileFormatException"></exception>
		/// <exception cref="Qdb.Ext.OldFormatException"></exception>
		/// <exception cref="Qdb.Ext.DatabaseReadOnlyException"></exception>
		public static IEmbeddedObjectContainer OpenFile(string databaseFileName)
		{
			return OpenFile(NewConfiguration(), databaseFileName);
		}
	}
}
